function FillFilter(){
	FillRoomFilterStatus();
	FillHouseKeeper();
	FillFilterDropdown();
	FillFilterCriteria();
	SortTable(houseKeeper,RenderHouseKeepingScreen);
}

function FillRoomFilterStatus(){
	$("[name=systemdate]").html(fullFormatedDate(getServerDateTime()));
	$("[name=edithousekeepingstatus]")[0].EasyDropDown.clearItem();
	for(j=0;j<SharedData.CleaningStatus.length;j++){
		$("[name=edithousekeepingstatus]")[0].EasyDropDown.addItem(SharedData.CleaningStatus[j].id,getLocalizeCleaningStatus(SharedData.CleaningStatus[j].title))
	}
}

function fullFormatedDate(date){
	return $.datepicker.formatDate("D, "+SharedData.General.dateFormat.dateTimeFormat,date,{monthNamesShort:FullMonth,monthNames:FullMonth,dayNamesShort:ShortDay,dayNames:ShortDay})
}

function FillHouseKeeper(){
	$("[name=housekeepingcommand]")[0].EasyDropDown.clearItem();
	$("[name=housekeepingcommand]")[0].EasyDropDown.addItem(-1,label_NotAssigned);
	simpleservice("/userService",{func:"GetStaff",department:"HouseKeeping"},function(data){
		houseKeeper.HouseKeeper = JSON.parse(data);
		for(j=0;j<houseKeeper.HouseKeeper.length;j++)
		{
			var name = "";
			if(houseKeeper.HouseKeeper[j].firstName!=null) name = houseKeeper.HouseKeeper[j].firstName;
			if(houseKeeper.HouseKeeper[j].lastName!=null) name = name + " " + houseKeeper.HouseKeeper[j].lastName;
			$("[name=housekeepingcommand]")[0].EasyDropDown.addItem(houseKeeper.HouseKeeper[j].id,name)
		}
		if(houseKeeper.addfiltercriteria==null || houseKeeper.addfiltercriteria==false)
		{
			if(houseKeeper.HouseKeeper.length > 0){
				$("[name=filterby]")[0].EasyDropDown.addItem("staff",dd_filter_housekeeping,"group");
				for(j=0;j<houseKeeper.HouseKeeper.length;j++){
					var name = "";
					if(houseKeeper.HouseKeeper[j].firstName!=null) name = houseKeeper.HouseKeeper[j].firstName;
					if(houseKeeper.HouseKeeper[j].lastName!=null) name = name + " " + houseKeeper.HouseKeeper[j].lastName;
					$("[name=filterby]")[0].EasyDropDown.addItem(houseKeeper.HouseKeeper[j].id,name,"groupDetail")
				}
			}
			houseKeeper.addfiltercriteria = true;
		}
	});
}

function ChangeCleaningStatus(){
	var status = $("select[name=edithousekeepingstatus] option:selected").val();
	if(status == 0) return;
	if($("input[type=checkbox]:checked").not("[name=All]").length == 0) return;
	var list = $("input[type=checkbox]:checked").not("[name=All]");
	var str = "";
	for(var i=0;i<list.length;i++)
	{
		str += $(list[i]).parents("tr:first").attr("roomid") + ",";
	}
	str = str.substring(0,str.length-1);
	simpleserviceWithArgs("/roomService",{func:"changCleaningStatus",roomId:str,cleanStatus:status},function(data){
		var objArray = JSON.parse(data);
		var checkLogOut = checkSessionAdmin(objArray);
		if(checkLogOut)
			return;
		if(objArray.id!=null){	var tmp = []; tmp.push(objArray); objArray = tmp;}
		for(var i=0;i<objArray.length;i++)
		{
			var cleaning = getObjectFromSharedData(houseKeeper.data,objArray[i].id);
			cleaning.cleaningStatusId = objArray[i].cleaningStatusString;
		}
		RenderHouseKeepingScreen();
	});
}

function AssignStaff(){
	var userId = $("select[name=housekeepingcommand] option:selected").val();
	if(userId == 0) return;
	var list = $("input[type=checkbox]:checked").not("[name=All]");
	var str = "";
	for(var i=0;i<list.length;i++)
	{
		if($(list[i]).parents("tr:first").find("td:last").attr("name")=="staffnm")
			str += $(list[i]).parents("tr:first").attr("roomid") + ",";
	}
	if(str == "") return;
	var roomId = str.substring(0,str.length-1);
	componentLoading($(".tableBorder"));
	simpleserviceWithError("/houseKeepingService",{func:"UpdateHouseKeeping",roomId:roomId,userId:userId},
	function(data,obj){
		closeComponentLoading($(".tableBorder"));
		var objArray = JSON.parse(data);
		var checkLogOut = checkSessionAdmin(objArray);
		if(checkLogOut)
			return;
		if(objArray.id!=null){	var tmp = []; tmp.push(objArray); objArray = tmp;}
		for(i=0;i<objArray.length;i++){
			var room = getObjectFromSharedData(houseKeeper.data,objArray[i].roomId);
			room.preference = objArray[i].reference;
			room.staff = objArray[i].staffString;
		}
		RenderHouseKeepingScreen();
	},
	function(data,obj){
		closeComponentLoading($(".tableBorder"));
	});
}

function FillFilterDropdown(){
	$("[name=filterby]")[0].EasyDropDown.clearItem();
	$("[name=filterby]")[0].EasyDropDown.addItem("All",dd_filter_allroom,"norm active")
	$("[name=filterby]")[0].EasyDropDown.addItem("cleaningStatusId",dd_filter_roomstatus,"group")
	for(var j=0;j<SharedData.CleaningStatus.length;j++){
		$("[name=filterby]")[0].EasyDropDown.addItem(SharedData.CleaningStatus[j].id,getLocalizeCleaningStatus(SharedData.CleaningStatus[j].title),"groupDetail")
	}
	
	if(SharedData.Block.length > 0){
		$("[name=filterby]")[0].EasyDropDown.addItem("block",dd_filter_block,"group");
		for(var j=0;j<SharedData.Block.length;j++){
			$("[name=filterby]")[0].EasyDropDown.addItem(SharedData.Block[j].id,SharedData.Block[j].title,"groupDetail")
		}
	}
	
	if(SharedData.Floor.length > 0){
		$("[name=filterby]")[0].EasyDropDown.addItem("floor",dd_filter_block,"group");
		for(var j=0;j<SharedData.Floor.length;j++){
			$("[name=filterby]")[0].EasyDropDown.addItem(SharedData.Floor[j].id,SharedData.Floor[j].title,"groupDetail")
		}
	}
}

function FillFilterCriteria(){
	$("[name=filterby]")[0].EasyDropDown.onChange = function(e){
		var filterby = $("[name=filterby]")[0].EasyDropDown.inGroup().attributes["value"].value;
		houseKeeper.filterby = filterby;
		houseKeeper.filterid = e.value;
		RenderHouseKeepingScreen();
	};
}

function toggle(e,openelm){
	var ele = openelm;
	var name = $(e).attr('title');
	if(ele.css('display') != "none") {
		ele.css('display' ,"none");
		$(e).html("+ "+name);
  	}
	else {
		ele.css('display', "block");
		$(e).html("- "+name);	  
		var CleaningStatusText="";
		if($(e).attr('name') == "toggle_item_cleaning"){
			for(var i =0;i<SharedData.CleaningStatus.length;i++){
				CleaningStatusText+='<div class="roomandcleaningmargin"><span><span style="background-color:'+SharedData.CleaningStatus[i].color+';" class="markcolor">&nbsp;</span>'+SharedData.CleaningStatus[i].title+'<span></div>'
			}
			ele.html(CleaningStatusText);
		}else if($(e).attr('name') == "toggle_item_room")
		{
			for(var i =0;i<SharedData.ResvStatus.length;i++){
				CleaningStatusText+='<div class="roomandcleaningmargin"><span><span style="background-color:'+SharedData.ResvStatus[i].color+';" class="markcolor">&nbsp;</span>'+SharedData.ResvStatus[i].title+'<span></div>'
			}
			ele.html(CleaningStatusText);
		}else{
			ele.css("min-height",32)
			componentLoading(ele);
			simpleserviceWithError("/DNRService",{func:"GetExistDNR"},function(data){
				closeComponentLoading(ele);
				var objArray = JSON.parse(data);
				var DNRData = "";
				for(var i =0;i<objArray.length;i++){
					var room = getObjectFromSharedData(SharedData.Room,objArray[i].roomId);
					var datefrom = new Date(objArray[i].date).getFormatedDateAndMonthString();
					var dateto = new Date(objArray[i].dateTo).getFormatedDateAndMonthString();
					DNRData += '<div dnr="'+objArray[i].id+'" roomid="'+objArray[i].roomId+'" date="'+new Date(objArray[i].date).toSystemFormat()+'" class="link tab" onClick="DNRonclick(this)">'+simpleLabel_room+'#'+room.roomName+' : '+datefrom+' - '+dateto+'</div>';
				}
				ele.html(DNRData);
			},function(data){
				closeComponentLoading(ele);
			});
		}
	}
} 

function DNRonclick(el){
	window.currentDNRElement = el;
	$.getScriptIfNotExist('/js/components/timespan/DNR.js',function(script,textStatus,e){
		var el = window.currentDNRElement;
		delete window.currentDNRElement;
		showDNRDialog($(el),$(el).attr("roomid"),$(el).attr("date"));
	})
}